'use strict';
module.exports = {
	/**
	 * XXXnameXXX
	 * @url admin/auto_run/pub/compute_tvmrsj_data 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		const db = uniCloud.database();
		const $ = db.command.aggregate;
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			_
		} = util;
		let {
			uid,
			tv_name,
			item,
			todayStart,
			todayEnd,
			last_StartTime,
			last_EndTime,
			yesterday
		} = data;
		let res = {
			code: 0,
			msg: ""
		};
		// 业务逻辑开始-----------------------------------------------------------
		if (!tv_name) {
			return;
		}

		//查询昨日该剧付费笔数
		res.rechardansu = await vk.baseDao.count({
			dbName: "a_recharge",
			whereJson: {
				tv_name: tv_name,
				pay_status: 1,
				_add_time: _.and(_.gt(last_StartTime), _.lt(last_EndTime))
			}
		});


		//查询昨该剧充值总金额
		res.rechargeNumber = await vk.baseDao.sum({
			dbName: "a_recharge",
			fieldName: "shifu_amount", // 需要求和的字段名
			whereJson: {
				tv_name: tv_name,
				pay_status: 1,
				_add_time: _.and(_.gt(last_StartTime), _.lt(last_EndTime))
			}
		});
		//ios查询昨该剧充值总金额
		res.rechargeNumber_ios = await vk.baseDao.sum({
			dbName: "a_recharge",
			fieldName: "shifu_amount", // 需要求和的字段名
			whereJson: {
				os_name: "ios",
				tv_name: tv_name,
				pay_status: 1,
				_add_time: _.and(_.gt(last_StartTime), _.lt(last_EndTime))
			}
		});
		//android查询昨该剧充值总金额
		res.rechargeNumber_android = await vk.baseDao.sum({
			dbName: "a_recharge",
			fieldName: "shifu_amount", // 需要求和的字段名
			whereJson: {
				os_name: "android",
				tv_name: tv_name,
				pay_status: 1,
				_add_time: _.and(_.gt(last_StartTime), _.lt(last_EndTime))
			}
		});
		//查询昨该剧充值总金额结束

		//查询昨日该剧付费人数
		let rechargeUsers = await db.collection('a_recharge')
			.aggregate()
			.match({
				tv_name: tv_name,
				_add_time: _.and(_.gt(last_StartTime), _.lt(last_EndTime)),
				pay_status: 1
			})
			.group({
				_id: null,
				user_count: $.addToSet('$user_id')
			})
			.end()
		res.rechargeUser = rechargeUsers.data.length ? rechargeUsers.data[0].user_count.length : 0
		//查询昨日该剧付费人数结束

		//查询昨日该剧付费新用户人数
		let todayrechargeUsers = await db.collection('a_recharge')
			.aggregate()
			.match({
				tv_name: tv_name,
				user_register_date: _.and(_.gt(last_StartTime), _.lt(last_EndTime)),
				_add_time: _.and(_.gt(last_StartTime), _.lt(last_EndTime)),
				pay_status: 1
			})
			.group({
				_id: null,
				user_count: $.addToSet('$user_id')
			})
			.end()
		res.todayrechargeUser = todayrechargeUsers.data.length ? todayrechargeUsers.data[0].user_count.length :
			0
		//查询昨日该剧付费新用户人数结束


		//查询昨日新用户充值总金额
		res.todayrechargeNumber = await vk.baseDao.sum({
			dbName: "a_recharge",
			fieldName: "shifu_amount", // 需要求和的字段名
			whereJson: {
				tv_name: tv_name,
				pay_status: 1,
				user_register_date: _.and(_.gt(last_StartTime), _.lt(last_EndTime)),
				_add_time: _.and(_.gt(last_StartTime), _.lt(last_EndTime))
			}
		});
		//查询昨日新用户充值总金额ios
		res.todayrechargeNumber_ios = await vk.baseDao.sum({
			dbName: "a_recharge",
			fieldName: "shifu_amount", // 需要求和的字段名
			whereJson: {
				os_name: "ios",
				tv_name: tv_name,
				pay_status: 1,
				user_register_date: _.and(_.gt(last_StartTime), _.lt(last_EndTime)),
				_add_time: _.and(_.gt(last_StartTime), _.lt(last_EndTime))
			}
		});
		//查询昨日新用户充值总金额android
		res.todayrechargeNumber_android = await vk.baseDao.sum({
			dbName: "a_recharge",
			fieldName: "shifu_amount", // 需要求和的字段名
			whereJson: {
				os_name: "android",
				tv_name: tv_name,
				pay_status: 1,
				user_register_date: _.and(_.gt(last_StartTime), _.lt(last_EndTime)),
				_add_time: _.and(_.gt(last_StartTime), _.lt(last_EndTime))
			}
		});
		//查询昨日新用户充值总金额结束

		//查询这部剧的观看人数开始
		let Usersview = await db.collection('a_vip_history')
			.aggregate()
			.match({
				tv_name: tv_name,
				_add_time: _.and(_.gt(last_StartTime), _.lt(last_EndTime))
			})
			.group({
				_id: null,
				user_id: $.addToSet('$user_id')
			})
			.end()
		res.Usersviews = Usersview.data.length ? Usersview.data[0].user_id.length :
			0
		console.log("观看本剧的人数=", res.Usersviews);
		//查询这部剧的观看次数结束

		//观看人数为0直接结束
		if (res.Usersviews == 0) {
			console.log(tv_name, "昨日该剧观看人数为0");
			return res;
		}
		//查询这部剧的完播率开始


		//获得这部剧的最后一集为多少集
		let tv_from = await db.collection('a_tv').where({
			tv_name: tv_name
		}).get()

		let series = tv_from.all_series
		let Usersview_end = await db.collection('a_vip_history')
			.aggregate()
			.match({
				tv_name: tv_name,
				_add_time: _.and(_.gt(last_StartTime), _.lt(last_EndTime)),
				series: series
			})
			.group({
				_id: null,
				user_id: $.addToSet('$user_id')
			})
			.end()
		//观看最后一集的人数
		let Usersview_ends = Usersview_end.data.length ? Usersview_end.data[0].user_id.length : 0
		//计算完播率 完播率 = 观看的最后一集的人数/观看人数
		res.End_broadcasting = Usersview_ends / res.Usersviews


		//查询这部剧的完播率结束


		//添加到数据库
		let add_datas = await db.collection('a_datas').add({
			tv_name: tv_name,
			todayrechargeNumber: res.todayrechargeNumber / 100,
			todayrechargeNumber_android: res.todayrechargeNumber_android / 100,
			todayrechargeNumber_ios: res.todayrechargeNumber_ios / 100,
			todayrechargeUser: res.todayrechargeUser,
			rechargeUser: res.rechargeUser,
			rechargeNumber: res.rechargeNumber / 100,
			rechargeNumber_android: res.rechargeNumber_android / 100,
			rechargeNumber_ios: res.rechargeNumber_ios / 100,
			rechardansu: res.rechardansu,
			Usersviews: res.Usersviews,
			End_broadcasting: res.End_broadcasting,
			date: yesterday,
			date_time: last_StartTime, //添加一个记录日期的时间戳，方便后面使用筛选日期功能
			type: 15, //0是平台总数据，1是账号级数据，2是链接级数据
			remark: "type=15,为剧每日数据",
			_add_time: new Date().getTime(),
			_add_time_str: vk.pubfn.timeFormat(new Date(), "yyyy-MM-dd hh:mm:ss")
		})
		if (add_datas.id) {
			//通过判断是否存在add_datas.id，来确认数据是否添加成功，从而调用自定义函数更新数据
			await updateAgents()
		}

		console.log("添加剧每日数据数据结果=", add_datas);

		// 业务逻辑结束-----------------------------------------------------------
		return res;

		//自定义函数
		async function updateAgents() {
			let updateAgents = await vk.baseDao.update({
				dbName: "a_tv", // 表名
				whereJson: { // 条件
					_id: item._id, // true：存在 false：不存在
				},
				dataJson: { // 需要修改的数据
					isComputed: 1, //当天是否已经计算，0没有计算，1已经计算
				}
			});
			console.log("updateAgents=", updateAgents);
		}
		//自定义函数结束
	}
}